<?php

defined('BASEPATH') or die('forbidden');

/**
 * 辅助函数
 * @author  Karson
 * @name    function.php
 * @since   2013-4-8 13:46:01
 */

/**
 * 获取配置选项
 * @param string $item
 * @return string
 */
function get_config_item($item) {
    $framework = &get_instance('controller');
    $result = $framework->cache->get("config_item_{$item}");
    if (!$result) {
        $one = $framework->db->one("SELECT * FROM {pre}config WHERE name='{$item}'");
        $result = is_array($one) ? $one['content'] : '';
        $framework->cache->set("config_item_{$item}", $result);
    }
    return $result;
}

function get_full_url($url) {
    $url = substr($url, 0, 4) != 'http' ? 'http://' . $url : $url;
    return $url;
}

/**
 * 发送到客户端
 * @param string $code 码
 * @param array $content 内容
 * @param mixed $userId 会员ID
 * @return boolean
 */
function call_client($code, $content = array(), $userId = null) {
    if (is_null($userId)) {
        $framework = &get_instance('controller');
        $userId = $framework->auth->user_id;
        if (!$userId)
            return false;
    }else if (is_array($userId)) {
        $userId = implode(',', $userId);
    }
    call_flash_client($userId, array(
        "Code" => $code,
        "ServerData" => $content)
    );
    return true;
}

/**
 * 发送系统消息到客户端
 * @param int $id ID(post表中的ID)
 * @param array $content 消息内容
 * @param int $channel 接收类别(1:世界 2:阵营 3:帮会 4:私聊 -1:系统(默认))
 */
function call_msg($id, $content = null, $channel = -1, $user_id = 0) {
    $msg = array(
        "id" => $id,
        "channel" => $channel,
        "content" => $content
    );
    return call_client('7_-3', $msg, $user_id);
}

/**
 * 调用API接口
 * @param string $url URL地址
 * @param string $code CODE码
 * @param array $data 发送的数据
 * @param int $user_id 发送的玩家
 * @return array('result'=>请求结果, 'msg'=>打印的数据)
 */
function call_api($url, $code, $data = array(), $user_id = null) {
    if (is_null($user_id)) {
        $framework = &get_instance('controller');
        $user_id = $framework->auth->user_id;
    }
    $params = array('v' => json_encode(array(
            "Code" => $code,
            "ServerData" => $data,
            "UserId" => $user_id
    )));
    $ret = post($url, $params);
    return $ret;
}

/**
 * 使用SOCKET通知到FLASH
 * @param int $user_id 会员ID 为0时表示发送到全部在线的会员
 * @param mixed $params 发送的参数
 */
function call_flash_client($user_id, $params = array()) {
    $framework = &get_instance('controller');
    $value = 'recal_form_server|';
    $value .= urlencode($params && is_array($params) ? json_encode($params) : '');
    try {
        $ip = "127.0.0.1";
        $ret = post("http://{$ip}:{$framework->config['socket_port']}/sm_to_client_phpstyle", "id={$user_id}&value={$value}");
        $result = $ret['result'] ? true : false;
    } catch (Exception $e) {
        $result = false;
    }
    return $result;
}

/**
 * 调用erlang的定时器,并回调一个URL
 * @param string $url 如果有中文,必须进行URLENCODE
 * @param mixed $params 参数
 * @param int $seconds 等待时间,默认为60秒
 * @return boolean 
 */
function call_erlang_callback($url, $params = array(), $seconds = 60) {
    $framework = &get_instance('controller');
    $url = stripos($url, 'http://') !== false ? $url : site_url($url, 1);
    $params = $params ? http_build_query($params) : '';
    $url .= $params && stripos($url, '?') !== false ? '&' . $params : ($params ? '?' . $params : '');
    try {
        $ip = "127.0.0.1";
        $ret = post("http://{$ip}:{$framework->config['socket_port']}/server_set_time_recode_phpstyle", "type=reach&nsyn_id=-1&time=" . (time() + $seconds) . "&url=" . urlencode($url));
        $result = $ret['result'] ? true : false;
    } catch (Exception $e) {
        $result = false;
    }
    return $result;
}